package com.zhuiyun.project.api.group.controller;

import com.zhuiyun.project.api.filemanage.service.FileManageService;
import com.zhuiyun.project.api.friend.service.FriendService;
import com.zhuiyun.project.api.group.entity.Group;
import com.zhuiyun.project.api.group.model.GroupModel;
import com.zhuiyun.project.api.group.service.GroupService;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

/**
 * @ClassName GroupController
 * @Description TODO
 * @Author kdj
 * @Date 2023/7/24 10:00
 **/
@RestController
@CrossOrigin
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;


    /**
     * @param file
     * @param groupModel
     * @Author kdj
     * @Description 创建群聊
     * @Date 2023/7/24 10:55
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @PostMapping("/createGroup")
    public CommonResult createGroup(MultipartFile file, GroupModel groupModel) {
        return groupService.createGroup(file, groupModel);
    }

    /**
     * @param createUserId
     * @Author kdj
     * @Description 获取群聊根据创始人
     * @Date 2023/7/24 16:03
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/getGroupByCreateUserId")
    public CommonResult getGroupByCreateUserId(int createUserId) {
        return groupService.getGroupByCreateUserId(createUserId);
    }

    /**
     * @param groupNum
     * @Author kdj
     * @Description 根据群号搜索到群 并判断是否添加过
     * @Date 2023/7/25 20:28
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/queryGroupByNum")
    public CommonResult queryGroupByNum(int groupNum, int userId, String type) {
        return CommonResult.ok(groupService.queryGroupByNum(groupNum, userId, type));
    }

    /**
     * @param groupId
     * @Author kdj
     * @Description 根据群id搜索到群 获取群信息
     * @Date 2023/7/25 20:28
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/queryGroupByGroupId")
    public CommonResult queryGroupByGroupId(int groupId) {
        return CommonResult.ok(groupService.queryGroupByGroupId(groupId));
    }

    /**
     * @param groupModel
     * @Author 韩佳轩
     * @Description 群聊首页信息
     * @Date 2023/7/26 21:48
     * @Return
     */
    @GetMapping("/queryGroupAll")
    public CommonResult queryGroupAll(GroupModel groupModel) {
        return CommonResult.ok(groupService.queryAllGroup(groupModel));
    }

    /**
     * @param userId
     * @param groupId
     * @Author kdj
     * @Description 判断该用户是否存在在该群聊
     * @Date 2023/7/29 20:53
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/isIngroup")
    public CommonResult isIngroup(int userId, int groupId) {
        return CommonResult.ok(groupService.isIngroup(userId, groupId));
    }


    /**
     * @Author 韩佳轩
     * @Description 修改头像和群名称
     * @Date 2023/7/27 19:26
     * @Return
     * @param group
     */
    @PutMapping("/updateGroup")
    public CommonResult updateGroup(MultipartRequest imageFile,Group group){
        MultipartFile file = imageFile.getFile("file");
        int i = groupService.updateGroupAll(file,group);
        if (i > 0) {
            return CommonResult.ok("修改成功");
        } else {
            return CommonResult.error("修改失败");
        }
    }
  /**
     * 查询群信息和群
     * @author 温
     * @date 2023/7/31 19:06
     * @parameter [groupId]
     * @return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/Group")
    public CommonResult Group(Integer groupId,Integer userId){
        GroupModel group = groupService.Group(groupId,userId);
        return CommonResult.ok(group);
    }
}
